/*
 * PasswordHasherTest.java
 *
 *  
 */
package org.msb.finance.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.nio.ByteBuffer;

import org.junit.Test;

/**
 * 
 * 
 * @author boudream
 * 
 */
public class PasswordHasherTest {

	/**
	 * Test method for {@link org.msb.finance.util.PasswordHasher#createBuffer(char[])}.
	 */
	@Test
	public void testCreateBuffer() {
		assertCorrectByteBuffer("fred123"); //$NON-NLS-1$
		assertCorrectByteBuffer(new String());
		assertCorrectByteBuffer("1"); //$NON-NLS-1$
		assertCorrectByteBuffer("thebiggestpassword,I d0n't_know-how+someone*could~remember=this"); //$NON-NLS-1$
	}

	private void assertCorrectByteBuffer(String password) {
		ByteBuffer buffer = PasswordHasher.createBuffer(password.toCharArray());
		StringBuilder result = new StringBuilder();
		int l = buffer.asCharBuffer().length();
		for (int i = 0; i < l; ++i) {
			result.append(buffer.asCharBuffer().get(i));
		}
		assertEquals(password, result.toString());
	}

	/**
	 * Test method for {@link org.msb.finance.util.PasswordHasher#translateToHex(byte[], boolean)}.
	 */
	@Test
	public void testTranslateToHex() {
		assertEquals(
				"00013a447f7d", PasswordHasher.translateToHex(new byte[] { 0x00, 0x01, 0x3a, 0x44, 0x7f, 0x7d }, false)); //$NON-NLS-1$
		assertEquals(
				"00013A447F7D", PasswordHasher.translateToHex(new byte[] { 0x00, 0x01, 0x3a, 0x44, 0x7f, 0x7d }, true)); //$NON-NLS-1$
	}

	/**
	 * 
	 */
	@Test
	public void testHash() {
		assertEquals("ce69b56b3e407c333b62fafb768d6d6e", PasswordHasher.hash("Fred123$".toCharArray())); //$NON-NLS-1$ //$NON-NLS-2$
		assertEquals("d41d8cd98f00b204e9800998ecf8427e", PasswordHasher.hash(new String().toCharArray())); //$NON-NLS-1$
		assertEquals("d41d8cd98f00b204e9800998ecf8427e", PasswordHasher.hash(null)); //$NON-NLS-1$

		assertTrue(PasswordHasher.hash(null).equals(PasswordHasher.hash(new String().toCharArray())));
	}
}
